﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Exepack;
using System.IO;

namespace ExepackTests
{
	/// <summary>
	/// Tests for TempFileManager class
	/// </summary>
	[TestClass]
	public class TempFileManagerTest
	{
		#region MS-Generated Code

		public TempFileManagerTest()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		#endregion

		[TestMethod]
		public void TempFileManager_FileInsideTemporaryDirectoryTest()
		{
			using (TempFileManager fm = new TempFileManager())
			{
				// create file inside temporary directory
				using (FileStream fs = File.Create(fm["someFile"]))
				{
					Assert.IsTrue(Path.GetDirectoryName(fs.Name) == fm.DirectoryName);
				}
			}
		}

		[TestMethod]
		public void TempFileManager_FileOutsideTemporaryDirectoryTest()
		{
			using (TempFileManager fm = new TempFileManager())
			{
				string tempPath = Path.GetTempPath();
				string tempFileName = Path.Combine(tempPath, Path.GetRandomFileName());

				// create file outside temporary directory
				using (FileStream fs = File.Create(fm[tempFileName]))
				{
					Assert.IsTrue(Path.GetDirectoryName(fs.Name) == Path.GetDirectoryName(tempPath));
				}
			}
		}

		[TestMethod]
		public void TempFileManager_FileCleanupTest()
		{
			List<string> files = new List<string>();

			using (TempFileManager fm = new TempFileManager())
			{
				string tempFileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

				// create file outside temporary directory
				using (FileStream fs = File.Create(fm[tempFileName]))
					files.Add(fs.Name);

				// create file inside temporary directory
				using (FileStream fs = File.Create(fm["cuckoo.txt"]))
					files.Add(fs.Name);
			}

			// check if all files are deleted
			foreach (string fileName in files)
			{
				Assert.IsFalse(File.Exists(fileName));
			}
		}

		[TestMethod]
		public void TempFileManager_FolderCleanupTest()
		{
			string tempDirectoryName;

			using (TempFileManager fm = new TempFileManager())
			{
				using (FileStream fs = File.Create(fm["cuckoo.txt"]))
				{
					tempDirectoryName = fm.DirectoryName;
				}
			}

			// check if temporary directory is deleted
			Assert.IsFalse(Directory.Exists(tempDirectoryName));
		}
	}
}
